﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 54DataTemplateDataTriggerTemplateSelector
{
    /// <summary>
    /// Interaction logic for DataTemplateSelector.xaml
    /// </summary>
    public partial class DataTemplateSelector : Window
    {
        public DataTemplateSelector()
        {
            InitializeComponent();
            List<Person> persons = new List<Person>();
            persons.Add(new Person() { Age = 10, Name = "Yin" });
            persons.Add(new Person() { Age = 11, Name = "Yang" });
            persons.Add(new Person() { Age = 12, Name = "Yo" });
            this.DataContext = persons;
        }
    }
    public class PersonHighlightTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item,
                                                    DependencyObject container)
        {
            Person person = item as Person;
            FrameworkElement element = container as FrameworkElement;

            if (person != null && element != null)
            {
                if (person.Name == "Yang")
                {
                    return element.FindResource("HighlightTemplate") as DataTemplate;
                }
                else
                {
                    return element.FindResource("DefaultTemplate") as DataTemplate;
                }
            }

            return null;
        }       
    }
}
